<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Places extends Controller {

	public $template='layout/index';

	public function action_onePlace() {
		if ($this->request->is_ajax()) {
			$id=$this->request->param('id');
			$place=Model::factory('places')->getOne($id, $this->request->param('type'));

			echo View::factory('pages/onePlace', array(
				'place' => $place
			));
		}
		else {
			$allCat = Model::factory('places')->getAll();

			foreach ($allCat as $item) {
				$allCatalog[$item['parent_id']][] = $item;
			}

			$menu = Model::factory('menu')->getMenu();

			$leftContent=View::factory('pieces/left_content', array(
				'catalog' => Model::factory('places')->get_countries(),
				'menu' => $menu
			));

			$id=$this->request->param('id');
			$place=Model::factory('places')->getOne($id, $this->request->param('type'));

			if (count($place)==0)
				throw new HTTP_Exception_404('Page not found');

			$rightContent=View::factory('pages/onePlace', array(
				'place' => $place
			));

	        if (!empty($_COOKIE['myRegionCountry'])) {
	            $cookie = explode('!', $_COOKIE['myRegionCountry']);
	            $myRegionCountry = $cookie[0];
	        }
	        else {
	            $myRegionCountry = 'Выбрать';
	        }

	        if (!empty($_COOKIE['myRegionCity'])) {
	            $cookie = explode('!', $_COOKIE['myRegionCity']);
	            $myRegionCountry = $cookie[0];
	        }

			$this->response->body(View::factory('layout/index', array(
				'header' => View::factory('pieces/header', array('myRegionCountry' => $myRegionCountry)),
				'title' => $place[0]['title'],
				'leftContent' => $leftContent,
				'rightContent' => $rightContent,
				'footer' => View::factory('pieces/footer', array(
					'menu' => $menu
				))
			)));
		}
	}
}